<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="org.springframework.web.context.WebApplicationContext"%>
<%@page import="org.springframework.web.context.support.WebApplicationContextUtils"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="db.ReceivepurmatDAO"%>
<%@page import="db.Receivepurmat"%>
<%@page import="db.BorreqDAO"%>
<%@page import="db.BormatDAO"%>
<%@page import="db.Borreq"%>
<%@page import="db.Bormat"%>
<%@ include file="/jsp/common/utility/logined.jsp" %>
<% 
	WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
	BorreqDAO borreqDao = (BorreqDAO)ctx.getBean("BorreqDAO");
	BormatDAO bormatDao = (BormatDAO)ctx.getBean("BormatDAO");
	ReceivepurmatDAO receivepurmatDao = (ReceivepurmatDAO)ctx.getBean("ReceivepurmatDAO");
	
	String reqCode = request.getParameter("reqCode");
	String reqDate = request.getParameter("reqDate");
	String tobId = request.getParameter("tobId");
	String projectId = request.getParameter("projectId");
	String reccompanyId = request.getParameter("reccompanyId");
	String companyId = request.getParameter("companyId");
	String remark = request.getParameter("remark");
	String recManager = request.getParameter("recManager");
	String giveManager = request.getParameter("giveManager");
	String maUserId = request.getParameter("maUserId");
	
	int uid = (Integer)session.getAttribute("id");
	int tid = (Integer)session.getAttribute("tid");
	SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
	
	Borreq borreq = new Borreq();
	try{
		borreq.setCompanyId(Integer.parseInt(companyId));
	}catch(Exception e){
		borreq.setCompanyId(0);
	}
	borreq.setReqDate(df.parse(reqDate));
	try{
		borreq.setTobId(Integer.parseInt(tobId));
	}catch(Exception e){
		borreq.setTobId(0);
	}
	try{
		borreq.setProId(Integer.parseInt(projectId));
	}catch(Exception e){
		borreq.setProId(0);
	}
	try{
		borreq.setReccompanyId(Integer.parseInt(reccompanyId));
	}catch(Exception e){
		borreq.setReccompanyId(0);
	}
	borreq.setRemark(remark);
	borreq.setTeamId(tid);
	borreq.setPhase(2);
	borreq.setMaState(1);
	borreq.setReqUserId(uid);
	borreq.setReqUserDate(new Date());
	borreq.setRecManager(recManager);
	borreq.setGiveManager(giveManager);
	
	if(maUserId != null && !"".equals(maUserId.trim())){
		borreq.setMaUserId(Integer.parseInt(maUserId));
	}
	
	borreqDao.save(borreq);
	
	borreq.setBorCode("B" + borreq.getId());
	borreqDao.attachDirty(borreq);
	
	String[] materialIds = request.getParameterValues("materialIds");
    String[] taskCodes = request.getParameterValues("taskCodes");
    String[] reqNums = request.getParameterValues("reqNums");
    
    for(int i=0;i<materialIds.length;i++){
    	Bormat bormat = new Bormat();
    	bormat.setDate(new Date());
    	bormat.setBorreqId(borreq.getId());
    	bormat.setReceivePurmatId(Integer.parseInt(materialIds[i]));
    	bormat.setReqNum(Integer.parseInt(reqNums[i]));
    	bormat.setState(1);
    	bormat.setTaskCode(taskCodes[i]);
    	bormatDao.save(bormat);
    	
    	Receivepurmat receivepurmat = receivepurmatDao.findById(Integer.parseInt(materialIds[i]));
    	receivepurmat.setUseNum(receivepurmat.getUseNum() - Integer.parseInt(reqNums[i]));
    	receivepurmatDao.attachDirty(receivepurmat);
    }
    
    request.setAttribute("borCode",borreq.getBorCode());
    request.setAttribute("info","success");
    pageContext.forward("/vm/borrowStock/borrowApp.vm");
%>